package com.jyw.flowable.controller;

import com.jyw.common.annotation.Log;
import com.jyw.common.core.controller.BaseController;
import com.jyw.common.core.domain.CommonResult;
import com.jyw.common.core.page.TableDataInfo;
import com.jyw.common.enums.BusinessType;
import com.jyw.flowable.domain.dto.*;
import com.jyw.flowable.domain.vo.WfModelVo;
import com.jyw.flowable.service.IWfModelService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * @author Created by ZhangCheng on 2023/11/10
 */
@RestController
@RequestMapping("/flowable/model")
public class WfModelController extends BaseController {

    @Resource
    private IWfModelService wfModelService;

    /**
     * 查询流程模型列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:model:list')")
    @GetMapping("/list")
    public TableDataInfo<WfModelVo> list(WfModelPageDto wfModelPageDto) {
        return wfModelService.list(wfModelPageDto);
    }


    /**
     * 查询模型流程
     *
     * @param modelId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('flowable:model:query')")
    @GetMapping(value = "/bpmnXml/{modelId}")
    public CommonResult getBpmnXml(@NotNull(message = "主键不能为空") @PathVariable("modelId") String modelId) {
        String xml=wfModelService.queryBpmnXmlById(modelId);
       return CommonResult.success("成功",xml);
    }

    /**
     * 新增流程模型
     */
    @PreAuthorize("@ss.hasPermi('flowable:model:add')")
    @Log(title = "流程模型", businessType = BusinessType.INSERT)
    @PostMapping(value = "add")
    public CommonResult add(@RequestBody WfModelAddDto wfModelAddDto) {
        wfModelService.add(wfModelAddDto);
        return CommonResult.success();
    }

    /**
     * 保存流程模型
     */
    @PreAuthorize("@ss.hasPermi('flowable:model:save')")
    @Log(title = "保存流程模型", businessType = BusinessType.UPDATE)
    @PostMapping(value = "save")
    public CommonResult save(@RequestBody WfModelSaveDto wfModelSaveDto) {
        wfModelService.save(wfModelSaveDto);
        return CommonResult.success();
    }

    /**
     * 部署流程模型
     *
     * @param wfModelDeployDto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('flowable:model:deploy')")
    @PostMapping("/deploy")
    public CommonResult deployModel(@RequestBody WfModelDeployDto wfModelDeployDto) {
        String modelId=wfModelDeployDto.getModelId();
        wfModelService.deployModel(modelId);
        return CommonResult.success();
    }

    /**
     * 修改流程模型
     */
    @PreAuthorize("@ss.hasPermi('flowable:model:edit')")
    @Log(title = "流程模型", businessType = BusinessType.UPDATE)
    @PutMapping(value = "edit")
    public CommonResult edit(@RequestBody WfModelEditDto wfModelEditDto) {
        wfModelService.edit(wfModelEditDto);
        return CommonResult.success();
    }

    /**
     * 删除流程模型
     */
    @PreAuthorize("@ss.hasPermi('flowable:model:remove')")
    @Log(title = "流程模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult remove(@PathVariable String[] ids) {
        wfModelService.deleteByIds(ids);
        return CommonResult.success();
    }
}
